<?php
$loader->requireOnce('includes/SearchingCore/ISearchController.class.php');
$loader->requireOnce('includes/SearchingCore/SearchFilter.class.php');
$loader->requireOnce('includes/SearchingCore/SearchEngine.class.php');
$loader->requireOnce('includes/SearchingCore/SearchResult.class.php');
$loader->requireOnce('includes/SearchingCore/SearchResultView.class.php');


/**
 * Controler for searching.
 * @package modules.dept_laboratory.controllers
 * @author viethd
 * @version $Id$
 * @copyright 2009 LHV Company
 */
class C_Search extends Controller implements ISearchController {
    private $logger ;
    
    /**
     * 
     * @var SearchEngine
     */
    protected $searchEngine;
	protected $searchFilter;
	protected $searchResultView;
	
    function __construct($template_mod = "general") {
        parent::Controller();
        $this->logger = LoggerManager::getLogger('local.controllers.C_Search');
    		 
        $this->searchEngine = new SearchEngine();
        $this->searchFilter = new SearchFilter();
        $this->searchResultView = new SearchResultView();
        
        $this->view->path="search";
    }
    

    public function actionSearchAjax($filterData = array()) {    	
        $this->logger->info('Start actionSearchAjax');
        $result = array('error'=>array('code'=>CoreErrorCode::NORMAL,'mess'=>''),'result'=>'');
        try {
        	$this->searchEngine->setSearchFilter($this->searchFilter);
        	$this->searchEngine->setSearchResultView($this->searchResultView);
        	
            $searchResult = $this->searchEngine->search($filterData);
            $result['result'] = $this->searchEngine->renderResult($searchResult);

        }  catch (SearchException $ex) {
            $result['error']['code'] = CoreErrorCode::COMMON_ERROR;
            $result['error']['mess'] = CoreErrorCode::getErrorMess(CoreErrorCode::COMMON_ERROR);
            $logger->error('actionSearchAjax fail. Error detail: '. $ex->getMessage());
        }

        $this->logger->info('Finish actionSearchAjax');
        return json_encode($result);
    }
    
    public function actionDisplaySearchPage() {
    	
    	$this->searchEngine->setSearchFilter($this->searchFilter);    	
        $searchFilterhtml = $this->searchEngine->renderFilter();
        
        $this->assign('searchFilter', $searchFilterhtml);
        $this->assign('searchAction', Celini::getCurrentController());
        $vp = $this->view->path;
        $this->view->path="search";
        $html = $this->view->render('search.html');
        $this->view->path=$vp;
        return $html;
    }


}
?>